<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page contentType="text/html;charset=UTF-8"
         language="java"
         import="com.compuvest.web.*,com.compuvest.webstatus.*" %>

<jsp:useBean id="sb" class="MySQL" scope="application"/>
<jsp:useBean id="mail" class="Mail" scope="application"/>
<jsp:useBean id="basket" class="Basket" scope="session"/>

<%!
    public static final int APPEND_OK = 0;
    public static final int APPEND_A = 1;
    public static final int APPEND_B = 2;
    public static final int APPEND_C = 3;
    public static final int APPEND_D = 4;
    public static final int APPEND_E = 5;

    public static final int NO_PAYMENT = 0;
    public static final int PAYMENT_CYBERSOURCE = 1;
    public static final int PAYMENT_PRECHARGE = 2;

    public static final String[] rules = {
/*0*/ "Your order is now being processed!\n",
/*1*/ "<b>If you are a new CompuVestB2B customer, please read the following:</b><br/><br/> 1. Since your billing address was different from shipping, you must fax a signed form authorizing us to ship the product to that particular shipping address. Please download the <a href='../images/form/form.rtf'><font color='red'>FORM</font></a>, print it, fill it out and fax to: 425-251-5000 Att: your order confirmation number, or sales order number.<br/>",
/*2*/ "<br/>2. You must call American Express customer service to add your \"ship to address\" on file as the Authorized Shipping address. This is a regular procedure for American Express. We always check if the credit card company has an authorized shipping address on cardholder's file before shipping the order.<br/>",
/*3*/ "<br/>2. You must call Discover customer service to add your \"ship to address\" on file as the Authorized Shipping address. This is a regular procedure for Discover. We always check if the credit card company has an authorized shipping address on cardholder's file before shipping the order.<br/>",
/*4*/ "<br/>2. Fax the front and back of the credit card used for the order to: 425-251-5000 along with the authorization form.<br/>",
/*5*/ "Your order exceeds $5000.00 and the shipping address is different from billing. In this case we do not guarantee the processing of the order as we need to verify the information you supplied. Someone will contact you shortly to go over payment details.<br/>"
    };

    public String checkPayment(Basket basket) {

	Customer cust = basket.getCustomer();

	if(!cust.compareBillingAndShipping() && basket.getPaymentMethod().equals("CC")) {

	    if(basket.getGrandTotal() >= 100d && basket.getGrandTotal() <= 700d) {

		if(cust.getCType().equals("VISA") || cust.getCType().equals("MASTERCARD")) return rules[APPEND_A];
		else if(cust.getCType().equals("AMEX")) return rules[APPEND_A] + rules[APPEND_B];
		else if(cust.getCType().equals("DISCOVER")) return rules[APPEND_A] + rules[APPEND_C];
		else return rules[APPEND_OK];
	    }
	    else if(basket.getGrandTotal() >= 700d && basket.getGrandTotal() <= 5000d) {

		if(cust.getCType().equals("VISA") || cust.getCType().equals("MASTERCARD")) return rules[APPEND_A] + rules[APPEND_D];
		else if(cust.getCType().equals("AMEX")) return rules[APPEND_A] + rules[APPEND_B];
		else if(cust.getCType().equals("DISCOVER")) return rules[APPEND_A] + rules[APPEND_C];
		else return rules[APPEND_OK];
	    }
	    else if(basket.getGrandTotal() >= 5000d) {
		if(cust.getCType().equals("VISA") || cust.getCType().equals("MASTERCARD") ||
		   cust.getCType().equals("AMEX") || cust.getCType().equals("DISCOVER")) {
		    return rules[APPEND_E];
		}
		return rules[APPEND_OK];
	    }
	}
	return rules[APPEND_OK];
    }

     Items basketItems = new Items(); // for getResellerPriceLine()
%>
<%
    if(basket.size() == 0) {
	response.sendRedirect( session.getAttribute("backItemsPage") == null ? application.getInitParameter("http") : (String)session.getAttribute("backItemsPage") );
	return;
    }

    Customer cust = basket.getCustomer();
    if(cust == null) {
	response.sendRedirect(application.getInitParameter("https") + "CustomerForm.jsp");
	return;
    }

    java.text.DecimalFormat priceForm = new java.text.DecimalFormat("$#,##0.00");
    java.text.DecimalFormat number = new java.text.DecimalFormat("0.00");
    java.text.SimpleDateFormat dateForm = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    String checkResult = checkPayment(basket);
    // for live statistic TODAY_USER
    int custID = cust.getCustomerID();

    // write order to database and update old customer or make new customer
//    String orderNumber = "200605087777";
    String orderNumber = sb.makeOrder(basket);
    basket.setOrderNumber(orderNumber);

    sb.setOrderStatus(orderNumber, ""); // write status auth order

    mail.sendEmail(basket);
%>
<html>
<%@include file="/incl/head.html" %>

<body style='background-color:white;'>
<%
    StringBuilder body = new StringBuilder();
    body.append("<div class='contentstyle'>\n");
    body.append("<fieldset style='padding:0;'>\n");
    body.append("<div class=cont>\n");
    body.append("<table width='99%'>\n");
    body.append("<tr>");
    body.append("<td colspan=2>");
    body.append("<img src='../images/hlogo.png'/>");
    body.append("<div class=tit style='text-align:center;'>Compuvestb2b.com&copy; Corporation, 3600 Lind Ave. S.W, Suite 130, Renton, WA 98057 USA.</div>");
    body.append("<br/><div class=tit>&nbsp;&nbsp;Tel: (425) 738-4091</div>");
    body.append("<div class=tit>&nbsp;&nbsp;Fax: (425)251-5000</div>");
    body.append("<div class=tit style='text-align:right;'>").append(dateForm.format(new java.util.Date())).append("&nbsp;&nbsp;&nbsp;</div>");
    body.append("<div class=tit>&nbsp;&nbsp;Reseller Certificate #: ").append(cust.getResellerCode()).append("</div>");
    if(cust.getAccwareAccount().length() != 0) {
       body.append("<div class='tit'>&nbsp;&nbsp;CompuvestB2B Account #: ").append(cust.getAccwareAccount()).append("</div>");
    }
    if(cust.isUseShippingAccount()) {
        body.append("<div class='tit'>&nbsp;&nbsp;Shipping Account #: ").append(cust.getShippingAccount()).append("</div>");
    }
    if(basket.getPurchaseOrder().length() != 0) {
        body.append("<div class='tit'>&nbsp;&nbsp;Purchase Order #: ").append(basket.getPurchaseOrder()).append("</div>");
    }
    body.append("<hr/>");
    body.append("<div class='tit' style='text-align:center;'><font size=2>Order <font color=red>").append(basket.getOrderNumber()).append("</font> Confirmation</font></div>");
    body.append("<br/>");
    body.append("</td>");
    body.append("</tr>\n");

    body.append(" <tr>");
    body.append("<td style='vertical-align:top;width:50%;'>");
    body.append("<div class='tit' style='padding:2px 6px;'>Billing Address:<br/>");
    body.append("<span style='font-weight:normal;'>");
    body.append(cust.billing.getCompany()).append("<br/>");
    body.append(cust.billing.getName()).append("<br/>");
    body.append(cust.billing.getAddress1()).append(' ');
    body.append(cust.billing.getAddress2()).append(' ');
    body.append(cust.billing.getCity()).append(",");
    body.append(cust.billing.getState()).append(' ');
    body.append(cust.billing.getZip()).append(' ');
    body.append(cust.billing.getZip4()).append(' ');
    body.append(cust.billing.getCountry()).append("<br/>");
    body.append("Tel: ").append(cust.billing.getPhone()).append("<br/>");
    if(cust.billing.hasCell())
	body.append("Cell: ").append(cust.billing.getCell()).append("<br/>");
    if(cust.billing.hasFax())
	body.append("Fax: ").append(cust.billing.getFax());
    body.append("</span></div>");
    body.append("</td>");
    body.append("<td style='vertical-align:top;width:50%;'>");
    body.append("<div class='tit' style='padding:2px 6px;'>").append(basket.getBlindShipping() ? "<font color=red>Blind</font> " : "").append("Shipping Address:<br/>");
    body.append("<span style='font-weight:normal;'>");
    body.append(cust.shipping.getCompany()).append("<br/>");
    body.append(cust.shipping.getName()).append("<br/>");
    body.append(cust.shipping.getAddress1()).append(' ');
    body.append(cust.shipping.getAddress2()).append(' ');
    body.append(cust.shipping.getCity()).append(",");
    body.append(cust.shipping.getState()).append(' ');
    body.append(cust.shipping.getZip()).append(' ');
    body.append(cust.shipping.getZip4()).append(' ');
    body.append(cust.shipping.getCountry()).append("<br/>");
    body.append("Tel: ").append(cust.shipping.getPhone()).append("<br/>");
    if(cust.shipping.hasCell())
	body.append("Cell: ").append(cust.shipping.getCell()).append("<br/>");
    if(cust.shipping.hasFax())
	body.append("Fax: ").append(cust.shipping.getFax());
    body.append("</span></div>");
    body.append("</td>");
    body.append("</tr>\n");

    body.append("<tr>");
    body.append("<td colspan=2><br/>");
    body.append("<table width='99%' class=part style='border-collapse:collapse' bordercolor=#d4d4d4 cellpadding=3 border=1>");
    body.append("<tr class='bsm sm' height=30><th width='11%'>Part #</th><th width='53%'>Product Name</th><th width='6%'>Qty</th><th width='10%'>Price</th><th width='10%'>Extended Warranty</th><th width='10%'>Amount</th></tr>");

    boolean ground = false;
    for(ItemsInBasket item : basket.getContents()) {

        body.append("<tr align=center height=30 bgcolor=#ffff").append((ground = !ground) ? "df" : "ff").append(">");
	body.append("<td id=nowrap>").append(item.getAccwareCode()).append("<div style='font-size:10px;color:blue;'>").append(item.getCondition()).append("</div></td>");
	body.append("<td align=left><div class=tlink>").append(item.getItemName()).append("</div>");
	body.append(basketItems.getResellerPriceLine(item)).append("</td>");
	body.append("<td>").append(item.getTotalQuantity()).append("</td>");
	body.append("<td>");
	if(item.getResPrice1() != 0d) {
	    body.append("<div class=prices>").append(priceForm.format(item.getPrice())).append("</div>");
	}
	body.append(priceForm.format(item.getSellPrice()));
	body.append("</td>");
	body.append("<td>").append(item.getWarrantyTotal() == 0d ? "" : priceForm.format(item.getWarrantyTotal()) + "<div style='font-size:10px;color:green;'>" + item.getWarrantyDesc() + "</div>").append("</td>");
	body.append("<td>").append(priceForm.format(item.getSellPrice() * item.getTotalQuantity() + item.getWarrantyPrice() * item.getTotalQuantity())).append("</td>");
	body.append("</tr>");
    }

    body.append("<tr><td colspan=6 align=right style='padding:8px 10px;'>Total Amount : ").append(priceForm.format(basket.getTotal())).append("</td></tr>");
    body.append("</table><br/>\n");

    body.append("<div class=tit style='text-align:center;'><font size=3>Summary</font></div>");
    body.append("<table width='99%' class=part style='border-collapse:collapse' bordercolor=#d4d4d4 cellpadding=3 border=1>");
    body.append("<colgroup><col width='60%'><col width='20%'><col width='20%'></colgroup>");
    body.append("<tr class='bsm sm' height=30><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th></tr>");

    if("CD".equals(basket.getPaymentMethod())) {
	body.append("<tr><td>Payment Method: C.O.D.</td><td>Charge</td><td align=right>$0</td></tr>");
    }
//    if("WA".equals(cust.shipping.getState())) {
//	body.append("<tr><td>WA Tax</td><td>Tax</td><td align=right>").append(priceForm.format(basket.getTaxCost())).append("</td></tr>");
//    }
//    if("PI".equals(basket.getPaymentMethod())) {
//	body.append("<tr><td>Transaction Fee</td><td>Charge</td><td align=right>").append(priceForm.format(basket.getTransFee())).append("</td></tr>");
//    }

    if(basket.getBlindShipping()) {
        body.append("<tr>");
        body.append("<td>Blind</td>");
        body.append("<td align=center>Shipping</td>");
        body.append("<td>&nbsp;</td>");
        body.append("</tr>");
    }
    if(basket.getPackingSlip()) {
        body.append("<tr>");
        body.append("<td>Slip</td>");
        body.append("<td align=center>Packing slip</td>");
        body.append("<td>&nbsp;</td>");
        body.append("</tr>");
    }
 
    body.append("<tr>");
    body.append("<td>Shipping Method: ").append(basket.getShippingDescription()).append("</td>");
    body.append("<td align=center>Shipping</td>");
    body.append("<td align=right>").append(priceForm.format(basket.getShippingCost())).append("</td>");
    body.append("</tr>");

    body.append("<tr>");
    body.append("<td colspan=2 align=left>Order Total : <div id='am'>&nbsp;</div></td>");
    body.append("<td id=nsu align=right valign=bottom>").append(priceForm.format(basket.getTempTotal() + basket.getTransFee())).append("</td>");
    body.append("</tr>");
    body.append("</table>");

    body.append("<img src='../images/pix.png' onload='NtoW();'/><br/>");
    body.append("<hr/>");
    body.append("<div class='tit' style='padding:2px 6px;font-weight:normal;'>").append(checkResult).append("</div>");
    body.append("<br/>");

    body.append("<div style='text-align:right;'>");
    body.append("<font size=2>Thank you for your Order.</font>&nbsp;&nbsp;&nbsp;&nbsp;");
    body.append("<button onclick='javascript:window.print();'>Print Order Confirmation</button>&nbsp;&nbsp;&nbsp;&nbsp;");
    body.append("<button onclick=\"javascript:location.href='").append(application.getInitParameter("http")).append("'\">Close page</button>&nbsp;&nbsp;");
    body.append("</div>");
    body.append("<br/>");

    body.append("</td>");
    body.append("</tr>");

    body.append("</table>\n");
    body.append("</div>\n");
    body.append("</fieldset>\n");
    body.append("</div>");

//    out.write(body.toString());
%>

<%
    if("GG".equals(basket.getPaymentMethod())) {
%>

<br/>
<table>
 <tr>
  <td width=160>&nbsp;</td>
  <td>
  <div style='font-size:12pt;'>
  <p><b>Thank You for Your Order!</b></p>
  <p><b>Your confirmation number is <font color=green face=arial size=4><%= basket.getOrderNumber() %></font></b></p>
  <p><font color=red>Your order is <b>NOT COMPLETE.</b><br/>
  Please click Google Checkout Button to finish your order.</font></p>

  <form action="https://checkout.google.com/cws/v2/Merchant/147336794269184/checkoutForm" accept-charset="utf-8" method="POST">
  <input type=hidden name="_charset_"/>
<%
	int count = 1;
	for(ItemsInBasket item : basket.getContents()) {
%>
  <input type=hidden name='item_name_<%= count %>" value="<%= item.getAccwareCode() %>'/>
  <input type=hidden name='item_description_<%= count %>" value="<%= item.getItemName() %>'/>
  <input type=hidden name='item_quantity_<%= count %>" value="<%= item.getTotalQuantity() %>'/>
  <input type=hidden name='item_price_<%= count %>" value="<%= number.format(item.getSellPrice()) %>'/>
<%
	    if(item.getWarrantyPrice() != 0d) {
		count++;
%>
  <input type=hidden name='item_name_<%= count %>' value='Extended warranty:'/>
  <input type=hidden name='item_description_<%= count %>' value='<%= item.getWarrantyDesc() %> for SKU #<%= item.getAccwareCode() %>'/>
  <input type=hidden name='item_quantity_<%= count %>' value='1'/>
  <input type=hidden name='item_price_<%= count %>' value='<%= number.format(item.getWarrantyTotal()) %>'/>
<%
	    }
	    count++;
	}
%>
  <input type=hidden name="ship_method_name_1" value="<%= basket.getShippingDescription() %>"/>
  <input type=hidden name="ship_method_price_1" value="<%= number.format(basket.getShippingCost()) %>"/>
  <input type=hidden name="tax_rate" value="0.089"/>
  <input type=hidden name="tax_us_state" value="WA"/>

  <input type=image name="Google Checkout" src="http://checkout.google.com/buttons/checkout.gif?merchant_id=147336794269184&w=180&h=46&style=trans&variant=text&loc=en_US" height="46" width="180"/>
 </form> 

  <p align='justify'><b>Please note:</b><br/>
  To ensure security, this order may require voice confirmation with our Customer Service Department prior to shipping.
  If this necessary, a representative will call or email you while processing you order. We reserve the right to cancel
  any order before it is shipped.
  </p>
   </div>
  </td>
  <td width=160>&nbsp;</td>
 </tr>
</table>
<hr/>

<%
	out.write(body.toString());

    }
    else if("PP".equals(basket.getPaymentMethod())) {
%>

<br/>
<table>
 <tr>
  <td width=160>&nbsp;</td>
  <td>
  <div style='font-size:12pt;'>
  <p><b>Thank You for Your Order!</b></p>
  <p><b>Your confirmation number is <font color=green face=arial size=4><%= basket.getOrderNumber() %></font></b></p>
  <p><font color=red>Your order is <b>NOT COMPLETE.</b><br/>
  Please click Paypal Checkout Button to finish your order.</font></p>

  <form action='https://www.paypal.com/cgi-bin/webscr' method='POST'>
  <input type=hidden name='cmd' value='_ext-enter'/>
  <input type=hidden name='redirect_cmd' value='_cart'/>
  <input type=hidden name='business' value='paypal@compuvest.com'/>
  <input type=hidden name='address_override' value='1'/>
  <input type=hidden name='no_note' value='1'/>
  <input type=hidden name='upload' value='1'/>
  <input type=hidden name='currency_code' value='USD'/>
  <input type=hidden name='first_name' value='<%= cust.shipping.getFirstName() %>'/>
  <input type=hidden name='last_name' value='<%= cust.shipping.getLastName() %>'/>
  <input type=hidden name='address1' value='<%= cust.shipping.getAddress1() %>'/>
  <input type=hidden name='address2' value='<%= cust.shipping.getAddress2() %>'/>
  <input type=hidden name='city' value='<%= cust.shipping.getCity() %>'/>
  <input type=hidden name='state' value='<%= cust.shipping.getState() %>'/>
  <input type=hidden name='zip' value='<%= cust.shipping.getZip() %>'/>
  <input type=hidden name='country' value='<%= cust.shipping.getCountry() %>'/>
  <input type=hidden name='email' value='<%= cust.getEmail() %>'/>
  <input type=hidden name='custom' value='Order <%= basket.getOrderNumber() %>'/>
<%
	int count = 1;
	for(ItemsInBasket item : basket.getContents()) {
%>
  <input type=hidden name='item_name_<%= count %>' value='<%= item.getItemName() %>'/>
  <input type=hidden name='quantity_<%= count %>' value='<%= item.getTotalQuantity() %>'/>
  <input type=hidden name='amount_<%= count %>' value='<%= number.format(item.getSellPrice()) %>'/>
  <input type=hidden name='item_number_<%= count %>' value='<%= item.getAccwareCode() %>'/>
<%
	    if(item.getWarrantyPrice() != 0d) {
		count++;
%>
  <input type=hidden name='item_name_<%= count %>' value='Extended warranty: <%= item.getWarrantyDesc() %> for SKU #<%= item.getAccwareCode() %>'/>
  <input type=hidden name='quantity_<%= count %>' value='1'/>
  <input type=hidden name='amount_<%= count %>' value='<%= number.format(item.getWarrantyTotal()) %>'/>
  <input type=hidden name='item_number_<%= count %>' value=''/>
<%
	    }
	    count++;
	}
%>
  <input type=hidden name='shipping_1' value='<%= number.format(basket.getShippingCost()) %>'/>
  <input type=hidden name='tax_cart' value='0'/>

  <input type=image src='https://www.paypal.com/en_US/i/btn/x-click-but03.gif' border=0 alt="Make payment PayPal - it's fast, free and secure!"/>
 </form>

  <p align=justify><b>Please note:</b><br/>
  To ensure security, this order may require voice confirmation with our Customer Service Department prior to shipping.
  If this necessary, a representative will call or email you while processing you order. We reserve the right to cancel
  any order before it is shipped.
  </p>
   </div>
  </td>
  <td width=160>&nbsp;</td>
 </tr>
</table>
<hr/>

<%
	out.write(body.toString());

    }
    else { // CC & PI & COD & Western Union 

	out.write(body.toString());

    } // all
%>


</body>

<%
    // backup order
    sb.copyToHistory(basket.getOrderID(), body.toString());

    // clean only basket
    basket.clean();
%>
<%@ include file="./incl/tracking.incl" %>
</html>
